import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
import * as compDiviService from '../../../services/AccountDivi';
const daoName = "COMP_JFQL";
const ButtonGroup = Button.Group;
const columns = [
  { title: '组织机构代码', dataIndex: 'ENCD', key: 'ENCD', width: 130, sorter: true, dispaly: true, canEdit: true, filter: true },
  { title: '客户名称', width: 300, sorter: true, dataIndex: 'CUNM', key: 'CUNM', dispaly: true, canEdit: true, filter: true },
  { title: '是否清除关联关系', key: 'ISQCGL', render: data => data.ISQCGL === true ? "是" : "否", width: 110, sorter: true, dispaly: true, filter: true },
];
let controls = [
  { name: 'ENCD', required: true, type: 'text', label: '组织机构代码', grid: 12 / 24 },
  { name: 'CUNM', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
  { name: 'ISQCGL', required: true, type: 'text', label: '是否清除关联关系', grid: 12 / 24 },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class KhjfqlForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }
    });
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 10 }
    });
    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  add() {
    let isok = true
    let cunm = ''
    let isDivi = ''
    const form = this.refs.editForm.getForm();
    form.validateFields((err, values) => {
      if (!err) {
        if (values.khmc === undefined || values.khmc === '') {
          message.error('客户名称不能为空！');
          isok = false
        }
        cunm = Number(values.khmc)
        isDivi = Number(values.isDivi)
      }
    });
    if (!isok) {
      return;
    }
    let content = isDivi === 0 ? "请确认该客户绩费清零？" : "请确认该客户绩费清零？清除关联关系后将无法恢复，请谨慎操作！"
    Modal.confirm({
      title: '提示',
      content: content,
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        compDiviService.compSetJfql({ cunm, isDivi }
        ).then(function (data) {
          if (data === 'OK') {
            message.success('客户添加成功！')
            this.props.dispatch({
              type: `${daoName}/fetch`,
              payload: { daoName, page: 1, pageSize: 10 }
            });
          } else {
            message.error('客户添加失败！' + data)
            return;
          }
        }.bind(this)).catch(function (ex) {
          console.log('请求出错', ex)
        });
      }
    });
  }
  deleteHandler() {
    const keys = this.props.selectedRowKeys;
    const len = keys.length;
    if (len === 0) {
      message.info('请选择要删除的行！');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: `你确认要删除选中的${len}行数据吗？`,
      okText: '删除',
      cancelText: '取消',
      onOk: () => {
        this.props.dispatch({
          type: `${daoName}/remove`,
          payload: { daoName, keys },
        });
      }
    });
  }
  render() {
    let { showEdit, record, permID, popedomTypes } = this.props
    const controlsEdit = [
      {
        name: 'khmc', type: 'search', label: '客户名称', grid: 9 / 24,
        fetch: { daoName: "TCompClie", searchColumns: ['ENCD', 'Name'], optionTpl: 'Name', valueTpl: "ID" },
      },
      { name: "isDivi", label: "是否清除关联关系", data: [{ value: '0', label: '否' }, { value: '1', label: '是' }], grid: 5 / 24, type: "select", layout: { labelSpan: 15, wrapperSpan: 6 } },
      { name: 'bg_1', required: false, type: 'button-group', data: [{ value: '1', label: '添加', onClick: this.add.bind(this) }, { value: '2', label: '删除', onClick: this.deleteHandler.bind(this) }], grid: 4 / 24 }
    ]
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            <EditFrom ref="editForm" daoName={daoName} controls={controlsEdit} clearItemMargin={true} record={{ isDivi: "0" }} />
          </div>
          <DataTable ref="ItemTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record
  }
}
KhjfqlForm = connect(mapStateToProps)(KhjfqlForm)
export default KhjfqlForm;

let render = (props) => React.createElement(KhjfqlForm, props);
let f = new Factory()
f.set("KhjfqlForm", render)
